package android.features.services;

import android.app.IntentService;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.provider.CallLog.Calls;
import android.text.format.DateUtils;
import android.util.Log;

public class CallFinderService extends IntentService {

	public CallFinderService() {
		super("CallFetcherService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {

		String projection[] = new String[] {Calls.TYPE, Calls.NUMBER, Calls.DURATION, Calls.DATE};
		
		ContentResolver contentResolver = getContentResolver();
		
		Cursor cursor = contentResolver.query(Calls.CONTENT_URI, projection, null, null, null);
		
		if(cursor.moveToFirst()) {
			
			int typeIndex = cursor.getColumnIndex(Calls.TYPE);
			int numberIndex = cursor.getColumnIndex(Calls.NUMBER);
			int durationIndex = cursor.getColumnIndex(Calls.DURATION);
			int dateIndex = cursor.getColumnIndex(Calls.DATE);
			
			do {
				String number = cursor.getString(numberIndex);
				int type = cursor.getInt(typeIndex);
				long duration = cursor.getLong(durationIndex);
				long dateEpoch = cursor.getLong(dateIndex);
				String date = DateUtils.formatDateTime(getApplicationContext(), dateEpoch, 0);
				
				String print = "Numero: " + number + " Type: " + type + " Duration: " + duration + " Date: " + date;
				Log.d("TESTE", print);
			}
			while(cursor.moveToNext());
			
		}
		
		cursor.close();
		
		Log.d("TESTE", "Chamou o servico");
	}
}
